package com.kefu.call.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.call.model.ContactPlan;
import com.kefu.call.service.ContactPlanService;
import com.kefu.call.vo.ContactPlanInsertVo;
import com.kefu.call.vo.ContactPlanSearchVo;
import com.kefu.call.vo.ContactPlanUpdateVo;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.exception.BusinessException;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(tags = "联系计划")
@RestController
@RequestMapping("call/activity/plan")
public class ContactPlanController extends AbstractBaseController {
    @Resource
    private ContactPlanService contactPlanService;

    @PostMapping("/add")
    @ApiOperation(value = "新增联系计划", notes = "新增联系计划")
    public Result<String> add(@Validated @RequestBody ContactPlanInsertVo contactPlanVo) {
    	logger.info("新增联系计划:{}", contactPlanVo);
    	if (contactPlanVo.getSubscribeTime()
                <= DateUtil.current()) {
    	    throw BusinessException.msg( "预约时间不能小于当前时间");
        }
        String result = contactPlanService.addContactPlan(contactPlanVo);
        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除联系计划", notes = "删除联系计划")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
    	logger.info("删除联系计划:{}", delete);
        String info = contactPlanService.delContactPlan(delete.getId());
        return ResultGenerator.genSuccessResult(info);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改联系计划", notes = "修改联系计划")
    public Result<String> update(@Validated @RequestBody ContactPlanUpdateVo contactPlanVo) {
    	logger.info("修改联系计划:{}", contactPlanVo);
        contactPlanService.updateContactPlan(contactPlanVo);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "联系计划详情", notes = "联系计划详情")
    public Result<ContactPlan> detail(@Validated @RequestBody DetailVo detail) {
        return ResultGenerator.genSuccessResult(contactPlanService.findById(detail.getId()));
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询联系计划", notes = "查询联系计划")
    public Result<Page<ContactPlan>> list(@Validated @RequestBody ContactPlanSearchVo search) {    
        Page<ContactPlan> list = contactPlanService.searchLambdaPage(search);
        return ResultGenerator.genSuccessResult(list);
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出联系计划", notes = "导出联系计划")
    public void export(@Validated @RequestBody ContactPlanSearchVo search, HttpServletResponse response) throws IOException {
        contactPlanService.exportContactPlan(search, response);
    }
}
